package com.vincent.template.service.security.impl;

import com.vincent.template.commons.base.VctUser;
import com.vincent.template.domain.SysUserPo;
import com.vincent.template.repository.SysUserRepository;
import com.vincent.template.store.UserRoleAuthStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author Vincent Wang
 * @date 2020/12/30 16:32
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private SysUserRepository userRepository;
    @Autowired
    private UserRoleAuthStore userRoleAuthStore;

    @Override
    public UserDetails loadUserByUsername(String username) {
        SysUserPo user = userRepository.findByUsernameAndDeletedFalse(username);
        if (user == null) {
            throw new UsernameNotFoundException("username not found!");
        }
        return VctUser.generate(user, userRoleAuthStore.findRolesAndAuths(user.getId()));
    }
}
